/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utilidades;

import java.awt.Color;
import java.awt.event.KeyEvent;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import net.java.balloontip.BalloonTip;
import net.java.balloontip.styles.BalloonTipStyle;
import net.java.balloontip.styles.MinimalBalloonStyle;

/**
 *
 * @author Lelouch
 */
public class ValidaUF extends ValidaDecimal {
	JProgressBar pb;
	JTextField tf;
	boolean usado;
	BalloonTip bt;

	public JProgressBar getPb () {
		return this.pb;
	}

	public ValidaUF (JTextField tf, JProgressBar pb) {
		usado = false;
		this.pb = pb;
		this.tf = tf;
		bt = new BalloonTip (tf, "", new MinimalBalloonStyle (new Color (231, 225, 165), 2), BalloonTip.Orientation.LEFT_ABOVE, BalloonTip.AttachLocation.ALIGNED, 20, 10, true);
		bt.setVisible (false);
	}

	@Override
	public void keyTyped (KeyEvent e) {
		//super.keyTyped (e);
		bt.setVisible (false);
		char caracter = e.getKeyChar ();
		if ((caracter >= '0' && caracter <= '9') || caracter == KeyEvent.VK_BACK_SPACE || caracter == KeyEvent.VK_DELETE || caracter == '.') {
			System.out.println (tf.getText ());
			String concat;
			// Se concatena el TextField con la letra ingresada
			if (caracter == KeyEvent.VK_BACK_SPACE || caracter == KeyEvent.VK_DELETE) {
				concat = tf.getText ();
			}
			else {
				concat = tf.getText () + caracter;
			}
			if (!concat.equals ("") && concat.charAt (0) == '.') {
				bt.setText ("El valor no puede empezar con punto");
				bt.setVisible (true);
				e.consume ();
				return;
			}
			if (concat.matches ("[0-9]+\\.{0,1}[0-9]*")) {
				if (!usado) {
					pb.setValue (pb.getValue () + 1);
					usado = true;
				}
			}
			else if (concat.equals ("") && usado) {
				pb.setValue (pb.getValue () - 1);
				usado = false;
				return;
			}
			else {
				if (caracter == '.') {
					bt.setText ("Ya existe un punto decimal");
					bt.setVisible (true);
				}
				e.consume ();
				return;
			}
		}
		else {
			bt.setText ("Ingrese sólo valores monetarios");
			bt.setVisible (true);
			e.consume ();
			return;
		}


		/*if ((caracter < '0' || caracter > '9') && caracter != KeyEvent.VK_BACK_SPACE && caracter != '.') {
		e.consume ();
		}
		else if (!concat.matches ("[0-9]+\\.{0,1}[0-9]*")) // Si existe mas de un '.' ingresado, com otambien si se ingresa un '.' si esta vacio
		{
		e.consume ();// LANZAR ERROR,

		}
		else {
		if (caracter == KeyEvent.VK_BACK_SPACE && (tf.getText ().isEmpty () || tf.getText ().matches ("[0-9]\\.[0-9]*"))) {
		if (usado) {
		pb.setValue (pb.getValue () - 1);
		usado = false;
		}
		else {
		e.consume ();
		}
		}
		else {
		if (tf.getText ().isEmpty () && (caracter != KeyEvent.VK_BACK_SPACE && caracter != KeyEvent.VK_DELETE) && !usado && caracter != '.') {
		pb.setValue (pb.getValue () + 1);
		usado = true;
		}
		}
		}*/
		//System.out.println (tf.getText ());

	}

}



